<?php

namespace app\v1\controller;

use app\model\AnnModel;
use support\Request;
use support\Response;
use app\model\MiniParsedown;
/**
 * 公告控制器
 * 
 * @package app\v1\controller
 */
class AnnController
{
    /**
     * 获取公告列表
     * 
     * @param Request $request
     * @return Response
     */
    public function list(Request $request): Response
    {
        try {
            // 获取分页参数
            $page = (int)($request->get('page', 1));
            $limit = (int)($request->get('limit', 10));
            
            if ($page < 1) {
                throw new \Exception('页码参数错误');
            }
            
            if ($limit < 1 || $limit > 50) {
                throw new \Exception('每页数量参数错误');
            }

            // 查询公告列表
            $query = AnnModel::notDeleted()
                ->select(['id', 'ann_title', 'created_at'])
                ->orderBy('created_at', 'desc');

            $total = $query->count();
            $list = $query->offset(($page - 1) * $limit)
                ->limit($limit)
                ->get();

            // 格式化数据
            $formattedList = [];
            foreach ($list as $item) {
                $formattedList[] = [
                    'id' => $item->id,
                    'title' => $item->ann_title,
                    'created_at' => date('Y-m-d H:i', $item->created_at)
                ];
            }

            return json([
                'data' => [
                    'list' => $formattedList,
                    'total' => $total,
                    'page' => $page,
                    'limit' => $limit
                ],
                'msg' => '获取成功',
                'code' => 1
            ]);
        } catch (\Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 获取公告详情
     * 
     * @param Request $request
     * @return Response
     */
    public function detail(Request $request): Response
    {
        try {
            $id = (int)($request->get('id'));
            
            if (empty($id)) {
                throw new \Exception('公告ID参数错误');
            }

            // 查询公告详情
            $ann = AnnModel::notDeleted()
                ->where('id', $id)
                ->first();

            if (!$ann) {
                throw new \Exception('公告不存在');
            }

            $data = [
                'id' => $ann->id,
                'title' => $ann->ann_title,
                'content' => MiniParsedown::instance()->text($ann->ann_content),
                'created_at' => date('Y-m-d H:i', $ann->created_at)
            ];

            return json([
                'data' => $data,
                'msg' => '获取成功',
                'code' => 1
            ]);
        } catch (\Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }
} 